package com.wh.wisdomsite.xbox.client.service.pr;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wh.wisdomsite.xbox.common.Constants;
import com.wh.wisdomsite.xbox.common.annotation.NeedInSessionType;
import com.wh.wisdomsite.xbox.common.annotation.ServiceMethod;
import com.wh.wisdomsite.xbox.common.annotation.ServiceProvider;
import com.wh.wisdomsite.xbox.common.reponse.CommonResult;
import com.wh.wisdomsite.xbox.pojo.pr.GetProjectIn;
import com.wh.wisdomsite.xbox.pr.entity.Project;
import com.wh.wisdomsite.xbox.pr.service.IProjectService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户端项目controller
 *
 * @version V1.0
 * @Package: com.wh.wisdomsite.xbox.client.service.pr
 * @author: 丁奕
 * @date: 2018-09-19 14:20
 * @Copyright 公司版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2016
 **/
@ServiceProvider()
public class ProjectClientService {

    private static final Log logger = LogFactory.getLog(ProjectClientService.class);

    @Autowired
    private IProjectService iProjectService;

    /**
     * 通过省、市、区ID获取项目集合
     * @param in
     */
    @ServiceMethod(method = "wh.zhgd.pr.project.getList", title = "获取项目列表", version = "1.0", needInSession = NeedInSessionType.NO)
    public CommonResult getList(GetProjectIn in){
        logger.info("获取项目列表->>wh.zhgd.pr.project.getList->>开始");
        CommonResult commonResult = new CommonResult();
        List<Map<String,String>> resultList = new ArrayList<>();
        List<Project> projectList = new ArrayList<Project>();
        try{
            EntityWrapper<Project> ew = new EntityWrapper<>();
            ew.setEntity(new Project());
            if(StringUtils.isNotEmpty(in.getProvice())){
                ew.eq("prProvice", in.getProvice());
            }
            if(StringUtils.isNotEmpty(in.getCity())){
                ew.eq("prCity", in.getCity());
            }
            if(StringUtils.isNotEmpty(in.getDistrict())){
                ew.eq("prDistrict", in.getDistrict());
            }
            projectList = iProjectService.selectList(ew);
            for (Project project : projectList){
                Map<String, String> tempMap = new HashMap<>();
                tempMap.put("projectId", project.getPrId());
                tempMap.put("projectName", project.getPrName());
                tempMap.put("projectAdd", project.getPrAddres());
                resultList.add(tempMap);
            }
            commonResult.setCode(Constants.CODE_SUCCESS);
            commonResult.setMessage(Constants.CODE_SUCCESS_MSG);
            commonResult.setResult(resultList);
            logger.info("获取项目列表->>wh.zhgd.pr.project.getList->>结束");
        }catch (Exception e){
            logger.error("获取项目列表->>wh.zhgd.pr.project.getList->>异常："+e.getMessage());
            commonResult.setCode(Constants.CODE_FAIL);
            commonResult.setMessage(Constants.CODE_FAIL_MSG);
            in.getXboxRequestContext().setException(new Exception(e.getMessage()));
            e.printStackTrace();
        }
        return commonResult;
    }
}
